<?php
namespace app\api\controller;
use x\Xbase;
class Docs extends Xbase{
    /**
     * 调试工具
     * @param string $app 项目配置
     */
    public function index()
    {
        $nodes = $this->nodes();
        $user  = $this->db->query('select * from user order by id asc limit 1',[],1);
        $token = $user['app_token'];
        $d = ['nodes'=>$nodes, 'token'=>$token];
        $this->fetch($d,'',false);
    }
    //加密
    public function jiami(){
        if(is_post()){
            $d = $this->post;
            if($d['type']){
                $rs = decode($d['title']);
            }else{
                $rs = encode($d['title']);
            }
            suc(['data'=>$rs]);
        }
        $this->fetch([],'',false);
    }

    //获取
    public function getsign(){
        if(is_post()){
            $d = $this->post;
            suc(['data'=>sign($d,$d['app_key'])]);
        }
    }

    //节点
    private function nodes(){
        $file = scandir(dirname(__FILE__)."/../docsconfig/");
        unset($file[0]);
        unset($file[1]);
        $temp = [];
        foreach($file as $k => $f){
            $temp[$k] = include dirname(__FILE__)."/../docsconfig/".$f;
        }

        //组装前端需要的格式
        $nodes = [];
        foreach($temp as $val){
            $arr = ["name" => $val['name'],];
            foreach($val['list'] as $k => $v){
                $arr["children"][] = [
                    "name" => $k,
                    "data" => $v,
                ];
            }
            $nodes[] = $arr;
        }
        return json_encode($nodes,JSON_UNESCAPED_UNICODE);
    }

    //上传
    public function up(){
        $this->fetch([],'',false);
    }

    //配置
    public function conf(){
        t(c('type'));
    }
}
